<?php

require_once('Mail.php');
require_once('Mail/mime.php');
require_once('Mail/RFC822.php');
require_once('MIME/Type.php');

class FRM_Mail
{
	static protected $_mail;

	/**
	 * Initialize the module
	 *
	 * @return nothing
	 */
	static public function initialize()
	{
		$params = array( 'host'     => FRM_Config::get('MAIL','SMTP_HOST'),
		                 'auth'     => TRUE,
		                 'username' => FRM_Config::get('MAIL','SMTP_USER'),
		                 'password' => FRM_Config::get('MAIL','SMTP_PASS') );

		self::$_mail = Mail::factory('smtp',$params);
	}

	/**
	 * Send a mime email to a specified list of recipients and return
	 * the number of valid email adress it founds in the recipients list
	 *
	 * @param mixed $to can be an array of email adresses or a single
	 *     email address as a string. Any non valid email in this array
	 *     will be skipped
	 * @param string $from the originating email address as it will appear
	 *     in the 'from' field
	 * @param string $subject An UTF8 encoded string for the subject of the
	 *     email
	 * @param string $txt_body An UTF8 encoded text for the text content of
	 *     the email
	 * @param string $html_body An UTF8 encoded text for the HTML content of
	 *     the email. If empty, a single text email will be sent.
	 * @param mixed $attachments is an array of file path to send along as
	 *     attachments of the email. Only absolute paths are expected.
	 *     A single file name can also be used.
	 * @param mixed $images is an array of images filenames to embed inline in the mail
	 *     so they can be used in the HTML part of the message.
	 *     A single filename can also be used.
	 * @return int The number of valid email address found in the $to parameter
	 */
	static public function sendMail($to,$from,$subject,$txt_body = '',$html_body = '', $attachments = array(), $images = array())
	{
		$mail = new FRM_Mail();
		$mail->addRecipient($to);
		$mail->setSender($from);
		$mail->setSubject($subject);
		$mail->setText($txt_body);
		$mail->setHTML($html_body);
		$mail->addAttachment($attachments);
		$mail->addImage($images);
		return $mail->send();
	}

	/** Now for instance level (not class) functions, with factory **/

	protected $_recipients = array();
	protected $_sender;
	protected $_text;
	protected $_html;
	protected $_attachments = array();
	protected $_images = array();

	/** Factory function
	 */
	static public function create()
	{
		return new FRM_Mail();
	}

	public function addRecipient($recipient)
	{
		if (is_array($recipient))
		{
			$this->_recipients = array_merge($this->_recipients,$recipient);
		}
		else
		{
			$this->_recipients[] = $recipient;
		}
	}

	public function setSender($sender)
	{
		$this->_sender = strval($sender);
	}

	public function setSubject($subject)
	{
		$this->_subject = $subject;
	}

	public function setText($txt)
	{
		$this->_text = $txt;
	}

	public function setHTML($html)
	{
		$this->_html = $html;
	}

	public function addAttachment($file)
	{
		if (is_array($file))
		{
			$this->_attachments = array_merge($this->_attachments,$file);
		}
		else
		{
			$this->_attachments[] = $file;
		}
	}

	public function addImage($img)
	{
		if (is_array($img))
		{
			$this->_images = array_merge($this->_images,$img);
		}
		else
		{
			$this->_images[] = $img;
		}
	}

	public function send()
	{
		$new_to = array();
		foreach($this->_recipients as $addr)
		{
			if (Mail_RFC822::isValidInetAddress($addr) !== FALSE)
			{
				$new_to[] = $addr;
			}
		}
		// Recipients list string for headers
		$recipients_list = implode(',',$new_to);

		// Let's now build the message
		$hdrs = array(
			'From' => $this->_sender,
			'To' => $recipients_list,
			'Subject' => $this->_subject,
			);
		$options = array('head_charset'=>'utf-8');

		$mime = new Mail_mime("\n");

		if ($this->_text)
		{
			$mime->setTXTBody($this->_text);
			$options['text_charset'] = 'utf-8';
		}

		if ($this->_html)
		{
			$mime->setHTMLBody($this->_html);
			$options['html_charset'] = 'utf-8';
		}

		// Let's do attachments
		foreach($this->_attachments as $file)
		{
			$mime->addAttachment($file,MIME_Type::autoDetect($file));
		}

		// Now, images
		foreach($this->_images as $file)
		{
			$mime->addHTMLImage($file,MIME_Type::autoDetect($file));
		}

		// We get back proper body and headers
		$body = $mime->get($options);
		$hdrs = $mime->headers($hdrs);

		// Actual sending
		foreach($new_to as $addr)
		{
			self::$_mail->send($addr,$hdrs,$body);
		}

		return count($new_to);
	}
}